<template>
<div>
  <el-button type="primary" :loading="refreshLoading" @click="refreshIndex" :disabled="connStatus != '成功'"><i class="iconfont icon-refresh"></i>一键手动同步索引</el-button>
  <div>
    连接状态：{{ connStatus }}
    <br>
    集群名：{{ clusterName }}
    <br>
    节点名：{{ name }}
    <br>
    文档数：{{ num }}
  </div>
</div>
</template>

<script>
export default {
  data() {
    return {
      refreshLoading: false,
      connStatus: "连接中...",
      clusterName: "",
      name: "",
      num: 0
    }
  },
  methods: {
    connectTest() {
      this.$axios.get("http://127.0.0.1:9200")
        .then((response) => {
          if (response.status == 200) {
            this.connStatus = "成功"
            this.clusterName = response.data.cluster_name,
            this.name = response.data.name
          }
        })
        .catch((error) => {
          this.connStatus = "失败"
        })
    },
    getHitsTotal() {
      this.$axios.get("http://127.0.0.1:9200/_search")
        .then((response) => {
          console.log(response)
          if (response.status == 200) {
            this.num = response.data.hits.total
          }
        })
    },
    refreshIndex() {
      this.refreshLoading = true
      this.$axios.get(API.rootPath + "/goods/importIndex").then((response) => {
        this.refreshLoading = false
          if (response.data.status == 0) {
              this.$message({
                  message: "更新成功",
                  type: "success"
              })
          }
      });
    }
  },
  created() {
    this.connectTest()
    this.getHitsTotal()
  }
};
</script>

